package com.wannengbang.cloudleader.homepage;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.Adapter_ViewPager_Fragment;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.BaseFragment;
import com.wannengbang.cloudleader.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RankingListActivity extends BaseActivity {


    @BindView(R.id.view_leftIcon)
    View viewLeftIcon;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.tv_share_money)
    TextView tvShareMoney;
    @BindView(R.id.tv_return_money)
    TextView tvReturnMoney;
    @BindView(R.id.magic_indicator_team)
    MagicIndicator magicIndicatorTeam;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewPager_team)
    ViewPager viewPagerTeam;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<String> mDataList;
    private List<String> mTeamDataList;
    private Adapter_ViewPager_Fragment adapter_viewPager;
    private Adapter_ViewPager_Fragment adapter_viewPager_team;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_list);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        //团队
        mTeamDataList = new ArrayList<>();
        mTeamDataList.add("今日交易");
        mTeamDataList.add("今日激活");
        mTeamDataList.add("上月交易");
        mTeamDataList.add("上月激活");

        //ViewPager的适配器
        List<BaseFragment> fragmentListTeam = new ArrayList<>();
        for (String title : mTeamDataList) {
            RankingTeamListFragment fragment = new RankingTeamListFragment();
            Bundle arguments = new Bundle();
            arguments.putString(RankingTeamListFragment.ORDER_TITLE_NAME, title);
            fragment.setArguments(arguments);
            fragmentListTeam.add(fragment);
        }
        adapter_viewPager_team = new Adapter_ViewPager_Fragment(getSupportFragmentManager(), fragmentListTeam, mTeamDataList);
        viewPagerTeam.setAdapter(adapter_viewPager_team);
        //指示器适配
        initMagicIndicatorTeam();


        //个人
        mDataList = new ArrayList<>();
        mDataList.add("今日交易");
        mDataList.add("今日激活");
        mDataList.add("上月交易");
        mDataList.add("上月激活");

        //ViewPager的适配器
        List<BaseFragment> fragmentList = new ArrayList<>();
        for (String title : mDataList) {
            RankingListFragment fragment = new RankingListFragment();
            Bundle arguments = new Bundle();
            arguments.putString(RankingListFragment.ORDER_TITLE_NAME, title);
            fragment.setArguments(arguments);
            fragmentList.add(fragment);
        }
        adapter_viewPager = new Adapter_ViewPager_Fragment(getSupportFragmentManager(), fragmentList, mDataList);
        viewPager.setAdapter(adapter_viewPager);
        //指示器适配
        initMagicIndicator();
    }

    private void initMagicIndicatorTeam() {
        magicIndicatorTeam.setBackgroundResource(R.mipmap.ic_ranking_juxing);
        CommonNavigator commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTeamDataList == null ? 0 : mTeamDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTeamDataList.get(index));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setNormalColor(Color.parseColor("#FFFFFF"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FFFFFF"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPagerTeam.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                indicator.setLineWidth(UIUtil.dip2px(context, 60));
                indicator.setColors(Color.parseColor("#FFFFFF"));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicatorTeam.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicatorTeam, viewPagerTeam);
        viewPagerTeam.setOffscreenPageLimit(5);
    }

    private void initMagicIndicator() {
        magicIndicator.setBackgroundResource(R.mipmap.ic_ranking_juxing);
        CommonNavigator commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setNormalColor(Color.parseColor("#FFFFFF"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FFFFFF"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                indicator.setLineWidth(UIUtil.dip2px(context, 60));
                indicator.setColors(Color.parseColor("#FFFFFF"));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
        viewPager.setOffscreenPageLimit(5);
    }

    @OnClick({R.id.ll_left, R.id.tv_share_money, R.id.tv_return_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.tv_share_money:
                tvShareMoney.setTextColor(getResources().getColor(R.color.theme_color));
                tvShareMoney.setBackgroundResource(R.drawable.shape_left_conner_white4);
                tvReturnMoney.setTextColor(getResources().getColor(R.color.white));
                tvReturnMoney.setBackgroundResource(R.drawable.shape_right_conner_theme4);
                magicIndicatorTeam.setVisibility(View.VISIBLE);
                viewPagerTeam.setVisibility(View.VISIBLE);
                magicIndicator.setVisibility(View.GONE);
                viewPager.setVisibility(View.GONE);
                break;
            case R.id.tv_return_money:
                tvShareMoney.setTextColor(getResources().getColor(R.color.white));
                tvShareMoney.setBackgroundResource(R.drawable.shape_left_conner_theme4);
                tvReturnMoney.setTextColor(getResources().getColor(R.color.theme_color));
                tvReturnMoney.setBackgroundResource(R.drawable.shape_right_conner_white4);
                magicIndicatorTeam.setVisibility(View.GONE);
                viewPagerTeam.setVisibility(View.GONE);
                magicIndicator.setVisibility(View.VISIBLE);
                viewPager.setVisibility(View.VISIBLE);
                break;
        }
    }
}
